<template>
	<view :class="['page', { iphoneX: isIphoneX }]">
		<template v-if="token && isLogin">
			<template v-if="loadingState.default">
				<loading :loadingState="loadingState"></loading>
			</template>
			<template v-else>
				<view class="body">
					<view class="change-btn">
						<view class="select-data">设备名称：{{ dev_name }}</view>
					</view>
					<!-- 详情状态 -->
					<!-- <template v-if="false&&!isSetting">
						<view class="detail-wrap m-t2">
							<view class="detail-item">
								<view class="label">工作模式</view>
								<view class="detail-input">
									<view class="content">
										<view class="val t-r">{{ work_mode_ops[detail.work_mode].name }}</view>
									</view>
									<view class="icon">
										<view class="iconfont f-s26">&#xe653;</view>
									</view>
								</view>
							</view>
							<template v-if="detail.work_mode==1">
								<view class="detail-item" v-for="item in 8" :key="item">
									<view class="label">工作时长</view>
									<view class="detail-input">
										<view class="content">
											<view class="val t-r">{{ params.working_hours || '--' }}</view>
										</view>
										<view class="icon">
											<view class="iconfont f-s26">&#xe653;</view>
										</view>
									</view>
								</view>
							</template>
							<template v-else>
								<view class="detail-item" v-for="item in 8" :key="item">
									<view class="label">开关{{item+1}}</view>
									<view class="detail-switch">
										<switch color="#3399FF"
											:checked="params[`smart_switch${item+1}`]==1 ? true : false" disabled>
										</switch>
										<view class="state">{{ params[`smart_switch${item+1}`] == 1 ? '开' : '关' }}
										</view>
									</view>
								</view>
							</template>
						</view>
					</template> -->

					<!-- 设置状态 -->
					<template>
						<view class="form-wrap m-t2">
							<view class="form-item">
								<view class="label">工作模式</view>
								<picker class="form-picker" mode="selector" :range="work_mode_ops" range-key="name"
									:value="detail.work_mode" @change="saveSetting(1, $event.detail.value)">
									<view class="picker">
										<view class="select-data t-l">{{ work_mode_ops[detail.work_mode].name }}</view>
										<view class="icon">
											<view class="iconfont f-s26">&#xe653;</view>
										</view>
									</view>
								</picker>
							</view>
							<template v-if="detail.work_mode==1">
								<view class="form-item" v-for="(item,index) in time" :key="index">
									<view class="label">工作时长{{index+1}}</view>
									<view class="form-input">
										<view class="content">
											<!-- <view v-if="detail.work_mode == 0" class="val work-time">
												<view class="start">
													<view class="disabled">{{ time.start }}</view>
												</view>
												<view class="line">-</view>
												<view class="over">
													<view class="disabled">{{ time.end }}</view>
												</view>
											</view> -->
											<view class="val work-time">
												<view class="start">
													<picker mode="time" @change="changeTime($event, 1,index)">
														<view>{{ item.start }}</view>
													</picker>
												</view>
												<view class="line">-</view>
												<view class="over">
													<picker mode="time" @change="changeTime($event, 2,index)">
														<view>{{ item.end }}</view>
													</picker>
												</view>
											</view>
										</view>
									</view>
								</view>
							</template>
							<template v-else>
								<view class="form-item" v-for="(item,index) in 8" :key="index">
									<view class="label">{{detail[`switch_name${index+1}`]}}</view>
									<view class="detail-switch">
										<!-- @change="changeSwitch($event,item+1)" -->
										<switch color="#3399FF"
											:checked="params[`smart_switch${index+1}`]==1 ? true : false"
											@click="saveSetting(2,index+1)"
											disabled></switch>
										<view class="state">{{ params[`smart_switch${index+1}`] == 1 ? '开' : '关' }}
										</view>
									</view>
								</view>
							</template>
						</view>
					</template>
				</view>
				<!-- <view class="footer">
					<view class="fix-wrap bottom">
						<view v-if="!isSetting" class="submit-btn" @click="showSetting">设置</view>
						<view v-else class="submit-btn" @click="saveSetting">保存设置</view>
					</view>
				</view> -->
			</template>
		</template>
	</view>
</template>

<script>
	import formdemo from '@/components/form.vue';
	import loading from '@/components/loading-more.vue';
	export default {
		name: 'name',
		data() {
			return {
				dev_id: '',
				dev_name: '',
				isLogin: false,
				isIphoneX: getApp().globalData.isIphoneX,
				appid: getApp().globalData.appid,
				token: uni.getStorageSync('token'),
				userInfo: uni.getStorageSync('userInfo'),
				loginStateUpdate: false,
				isStop: true,
				loadingState: {
					default: true
				},
				isSetting: false,
				detail: {
					work_mode: '0'
				},
				params: {
					work_mode: 0,
					working_hours1: '',
					working_hours2: '',
					working_hours3: '',
					working_hours4: '',
					working_hours5: '',
					working_hours6: '',
					working_hours7: '',
					working_hours8: '',
					smart_switch1: '0',
					smart_switch2: '0',
					smart_switch3: '0',
					smart_switch4: '0',
					smart_switch5: '0',
					smart_switch6: '0',
					smart_switch7: '0',
					smart_switch8: '0'
				},
				work_mode_ops: [{
						value: '0',
						name: '手动模式'
					},
					{
						value: '1',
						name: '自动模式'
					}
				],
				time: [{
					start: '00:00',
					end: '00:00'
				}, {
					start: '00:00',
					end: '00:00'
				}, {
					start: '00:00',
					end: '00:00'
				}, {
					start: '00:00',
					end: '00:00'
				}, {
					start: '00:00',
					end: '00:00'
				}, {
					start: '00:00',
					end: '00:00'
				}, {
					start: '00:00',
					end: '00:00'
				}, {
					start: '00:00',
					end: '00:00'
				}],
				select: -1,
				camera_interval_data: [10, 20, 30, 40, 50, 60]
			};
		},
		async onLoad(params) {
			this.dev_id = params.dev_id;
			this.dev_name = params.dev_name;
			if (this.token) {
				this.isLogin = await this.$loginState('check');
				if (this.isLogin) {
					this.detail = await this.getDeviceSetDetail();
					this.setParams();
				} else {
					this.isLogin = await this.$loginState('reset');
				}
			}
		},
		async onShow() {
			if (this.loginStateUpdate) {
				this.token = uni.getStorageSync('token');
				this.userInfo = uni.getStorageSync('userInfo');
				this.isLogin = true;
				this.detail = await this.getDeviceSetDetail();
				this.setParams();
				this.loginStateUpdate = false;
			}

			if (!this.token && !this.isLogin) {
				this.isLogin = await this.$loginState('reset');
			}
		},
		methods: {
			// 获取设备管理详情
			getDeviceSetDetail: async function() {
				let res = await this.$api.tool_api.getDyDetail({
					appid: this.appid,
					token: this.token,
					device_id: this.dev_id,
					user_id: this.userInfo.uid
				}).finally(() => {
					this.loadingState.default = false;
				});
				if (res.data.ret == 1000) {
					return res.data.data.list;
				}
			},

			// 获取设备管理参数
			setParams: function() {
				if (this.detail.work_mode == 1) {
					for (let i = 0; i < 8; i++) {
						let time = this.detail[`working_hours${i+1}`]
						// this.params[`working_hours${i+1}`] = time
						this.$set(this.params, `working_hours${i+1}`, time)
						console.log(time.split('-'));
						this.time[i].start = time.split('-')[0] || '00:00'
						this.time[i].end = time.split('-')[1] || '00:00'
					}
				} else {
					for (let ix = 0; ix < 8; ix++) {
						this.params[`smart_switch${ix + 1}`] = this.detail[`smart_switch${ix + 1}`]
					}
				}
			},

			// 设置开始时间
			changeTime: function(e, type, ix) {
				this.select = ix
				if (type == 1) {
					this.time[ix].start = e.detail.value;
				} else if (type == 2) {
					this.time[ix].end = e.detail.value;
				}
				this.saveSetting(3)
				// this.params[`working_hours${ix}`] = this.time[ix].start + '-' + this.time[ix].end;

			},

			// 打开设置
			showSetting: function() {
				this.isSetting = !this.isSetting;
			},

			// 保存设置
			saveSetting: async function(t, ix) {
				if (this.isStop) {
					this.isStop = false;
					uni.showLoading({
						title: '保存中'
					});

					let params = {
						appid: this.appid,
						token: this.token,
						device_id: this.dev_id,
						user_id: this.userInfo.uid,
					}

					if (t == 1) {
						params = Object.assign({}, params, {
							work_mode: ix //this.detail.work_mode
						})
					} else if (t == 2) {
						params = Object.assign({}, params, {
							[`smart_switch${ix}`]: this.params[`smart_switch${ix}`] == 1 ? 0 : 1
							// [`smart_switch`]: ix
						})
					} else {
						params = Object.assign({}, params, {
							[`working_hours${this.select+1}`]: this.time[this.select].start + '-' + this.time[this.select].end //this.params[`working_hours${this.select+1}`]
						})
					}

					let res = await this.$api.tool_api.setDyManage(params).finally(() => {
						uni.hideLoading();
						this.isStop = true;
					});
					if (res.data.ret == 1000) {
						this.detail = await this.getDeviceSetDetail();
						this.setParams();
					} else {
						this.$showToast({
							icon: 'error',
							title: '设置失败'
						});
					}
				}
			}
		},
		components: {
			loading
		},
		computed: {},
		watch: {}
	};
</script>

<style lang="less">
	.submit-btn {
		height: 100rpx;
		line-height: 100rpx;
		text-align: center;
		background: #0085ff;
		color: #fff;
		font-size: 32rpx;
	}

	.work-time {
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 60rpx;
		line-height: 60rpx;
		font-size: 28rpx;

		.start,
		.over {
			flex-grow: 2;
			border: 0.1rpx solid #ddd;
			text-align: center;
		}

		.line {
			margin: 0 10rpx;
		}

		.disabled {
			background: #f5f5f5;
			color: #999;
		}
	}
</style>